
matrix orthoProjMatrix;
float3x4 transformMatrix;
float4 viewportSize;

texture brushTexture;

sampler brushSampler = sampler_state
{
	Texture = <brushTexture>;
	AddressU = CLAMP;
	AddressV = CLAMP;
	AddressW = CLAMP;
	BorderColor = float4(0, 0, 0, 0);
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
};

struct VertexShaderInput
{
	float3 position : POSITION0;
	float4 color : COLOR0;
	float2 texCoord : TEXCOORD0;
};

struct PixelShaderInput
{
	float4 position : POSITION0;
	float2 texCoord : TEXCOORD0;
	float4 color : TEXCOORD1;
};

void VertexMain(in VertexShaderInput vsInput, out PixelShaderInput output)
{
	/*output = (PixelShaderInput)0;

	output.position = mul(float4(vsInput.position, 1), orthoProjMatrix);
	output.texCoord = vsInput.texCoord;
	output.color = vsInput.color;*/

	output = (PixelShaderInput)0;

	float3 newPosTransform = mul(float3(vsInput.position.xy, 1), float3x3(transformMatrix._11_12_13, transformMatrix._21_22_23, transformMatrix._31_32_33));
	newPosTransform.xy /= newPosTransform.z;

	float4 posTransformed = float4(newPosTransform.xy, 0, 1);
	float xbase = -viewportSize.x / 2.0f;
	float ybase = viewportSize.y / 2.0f;
	posTransformed.x += xbase;
	posTransformed.y = ybase - posTransformed.y;

	output.position = mul(float4(posTransformed.xy, 0, 1), orthoProjMatrix);
	output.position.x -= 1.0f / (2 * viewportSize.x);
	output.position.y -= 1.0f / (2 * viewportSize.y);
	output.texCoord = vsInput.texCoord;
	output.color = vsInput.color;
}

float4 sampleColor(float2 texCoord) {
	float stepx = 1.0f / viewportSize.x;
	float stepy = 1.0f / viewportSize.y;

	float2 topLeft = float2(texCoord.x - stepx, texCoord.y - stepy);
	float2 topRight = float2(texCoord.x + stepx, texCoord.y - stepy);
	float2 bottomLeft = float2(texCoord.x - stepx, texCoord.y + stepy);
	float2 bottomRight = float2(texCoord.x + stepx, texCoord.y + stepy);

	float4 ret = tex2D(brushSampler, texCoord);
	ret += tex2D(brushSampler, topLeft);
	ret += tex2D(brushSampler, topRight);
	ret += tex2D(brushSampler, bottomLeft);
	ret += tex2D(brushSampler, bottomRight);

	ret /= 5.0f;
	return ret;
}

void PixelMainFill(in PixelShaderInput input, out float4 color : COLOR0)
{
	color = sampleColor(input.texCoord);
}